package mlz.d_tcp3;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

/*
    服务器端的读取数据的线程类
 */
public class ServerReaderThread extends Thread{
    private Socket socket;

    public ServerReaderThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {

        try {
            //从socket中获取出字节输入流
            InputStream is = socket.getInputStream();
            //把原来的字节流包装到数据流中
            DataInputStream dis = new DataInputStream(is);
            while(true){
                try {
                    //读取信息
                    String msg = dis.readUTF();
                    System.out.println(msg);
                } catch (IOException e) {
                    //提示客户端下线了
                    System.out.println("有客户端下线了~~" + socket.getRemoteSocketAddress());
                    dis.close();
                    socket.close();
                    break;
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /*//群聊 -> 一个服务器面对多个客户端(多个客户端之间也可以共享数据 -> 端口转发)
    private void sendMsgAll(String msg){
        //发送给全部在线的socket接受
        for(Socket s : Server.){

        }
    }*/

}










